package cn.tedu.medicalmanagementsystem.manager.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ManagerLoginParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ManagerUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ReviewQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.Manager;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.EscortVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ReviewVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTrackVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTypeVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;


@Slf4j
@RestController
@Api(tags = "管理员模块")
@RequestMapping("/v1/manager/")
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @PostMapping("login")
    @ApiOperation("管理员登录")
    public JsonResult login(@RequestBody ManagerLoginParam managerLoginParam, @ApiIgnore HttpSession session) {
        try {

            Manager manager = managerService.login(managerLoginParam);
            session.setAttribute("managerId", manager.getId());
            session.setAttribute("manager", manager);
            return JsonResult.ok(manager.getId());
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }

    @PostMapping("serviceTrack")
    @ApiOperation("服务跟踪功能")
    public JsonResult serviceTrack() {
        List<ServiceTrackVO> serviceTrackList = managerService.getServiceTracks();

        log.debug("管理员查询服务跟踪数据，共获取{}条记录", serviceTrackList.size());

        return JsonResult.ok(serviceTrackList);
    }

    @GetMapping("reviews")
    @ApiOperation("获取评价")
    public JsonResult listReviews() {
        List<ReviewVO> reviewList = managerService.getReviewList();

        log.debug("管理员查询陪诊评价数据，共获取{}条记录", reviewList.size());

        return JsonResult.ok(reviewList);
    }

    @PostMapping("/reviewSelect")
    @ApiOperation("搜索相关评价")
    public JsonResult listEscortReviews(@RequestBody ReviewQuery reviewQuery) {
        List<ReviewVO> escortReviewList = managerService.getReviewsByEscortId(reviewQuery);

        log.debug("管理员查询陪诊评价数据，共获取{}条记录", escortReviewList.size());

        return JsonResult.ok(escortReviewList);
    }

    @PostMapping("/delReviews")
    @ApiOperation("删除评价")
    public JsonResult delReviews(@RequestParam Long reviewId){
        managerService.delReviews(reviewId);
        return JsonResult.ok();
    }

    @GetMapping("/escort")
    @ApiOperation("获取待审核陪诊员信息")
    public JsonResult listEscort(
            @RequestParam String status) {
        List<EscortVO> escort = managerService.getEscortByStatus(status);
        log.debug("管理员查询等待审核的陪诊员信息，状态: {}", status);
        return JsonResult.ok(escort);
    }

    @PostMapping("/audit")
    @ApiOperation("陪诊员信息审核")
    public JsonResult auditEscort(
            @RequestParam Long escortId,
            @RequestParam String status,
            @RequestParam(required = false) String auditComment) {
        managerService.auditEscort(escortId, status, auditComment);
        log.debug("管理员审核陪诊员信息，ID: {}, 状态: {}, 审核意见: {}", escortId, status, auditComment);
        return JsonResult.ok();
    }

    @PostMapping("/personal")
    @ApiOperation("管理员获取个人资料")
    public JsonResult getPersonal(@ApiIgnore HttpSession session) {
        Manager manager = managerService.getCurrentManager(session);
        return JsonResult.ok(manager);
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员个人资料")
    public JsonResult updatePersonal(
            @RequestBody ManagerUpdateParam managerUpdateParam
    ) {
        managerService.updatePersonal(managerUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/serviceType")
    @ApiOperation("获取服务类型")
    public JsonResult getServiceType(){
        List<ServiceTypeVO> serviceTypeList = managerService.getServiceTypeList();
        return JsonResult.ok(serviceTypeList);
    }

    @PostMapping("/logout")
    @ApiOperation("管理员退出登录")
    public JsonResult logout(@ApiIgnore HttpSession session) {
        session.removeAttribute("managerId");
        session.removeAttribute("manager");
        return JsonResult.ok();
    }

}